﻿/*Hdip Project 2013-14, Student Name:- Chris Pembroke,  Student Number:- x00115839*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

using Exercise1Project.Models; // added namespace ref for models
using System.Net.Mail;  // added namespace for sending sms mail
using Exercise1Project.Models.Validation; //added namespace for validation


namespace Exercise1Project.Controllers
{
    public class EmailCustomerController : Controller
    {
        private IBusinessLogicService _serviceBike;
        private IBikeServiceBusinessLogic _serviceBikeService;
        private ICustomerDetailBusinessLogic _serviceCustomer;
        public const string bodyBusinessNameEnding = "\n\n Chris's Bike Shop Repair/Service" + "\n 47 Montague Plaza" + "\n Dublin 2";

        public EmailCustomerController()
        {
            _serviceBike = new BusinessLogicService(new ModelStateWrapper(this.ModelState));
            _serviceBikeService = new BikeServiceBusinessLogic(new ModelStateWrapper(this.ModelState));
            _serviceCustomer = new CustomerDetailBusinessLogic(new ModelStateWrapper(this.ModelState));
        }

        public EmailCustomerController(IBusinessLogicService serviceBike, IBikeServiceBusinessLogic serviceBikeService, ICustomerDetailBusinessLogic serviceCustomer)
        {
            _serviceBike = serviceBike;
            _serviceBikeService = serviceBikeService;
            _serviceCustomer = serviceCustomer;
        }

        //
        // GET: /EmailCustomer/Create
        public ActionResult CreateEmail(int id)
        {
            //get all the needed info on bike, bikeservice, customer
            BikeService serviceDetail = _serviceBikeService.GetBikeServiceByServiceId(id);
            Bike bikeDetail = _serviceBike.GetBikeByBikeId(serviceDetail.BikeID);
            User customerDetail = _serviceCustomer.GetCustomerDetailsByCustomerId(bikeDetail.CustomerID);

                ViewBag.bikeMake = bikeDetail.Make;
                ViewBag.bikeModel = bikeDetail.Model;
                ViewBag.bikeColor = bikeDetail.Color;
                ViewBag.bikeId = bikeDetail.BikeID;

                ViewBag.customerName = customerDetail.Name;
                ViewBag.customerAddress = customerDetail.Address;
                ViewBag.customerEmail = customerDetail.Email;
                ViewBag.customerPhone = customerDetail.Phone;

                ViewBag.bikeServiceId = serviceDetail.ServiceID;
                ViewBag.bikeServiceSelectedItems = serviceDetail.SelectedItems;
                ViewBag.bikeServiceDate = serviceDetail.Date;
                ViewBag.bikeServiceStatus = serviceDetail.StatusService;
                ViewBag.bikeServiceStatusBikeInShop = serviceDetail.StatusBikeInShop;
                ViewBag.bikeServiceLabourCost = serviceDetail.LabourCost;
                ViewBag.bikeServicePartsCost = serviceDetail.PartsCost;
                ViewBag.bikeServiceTotalPrice = serviceDetail.TotalPrice;
                ViewBag.bikeServiceQuotedPrice = serviceDetail.QuotedPrice;

                string bodyBusinessNameEnding = "\n\n Chris's Bike Shop Repair/Service" + "\n 47 Montague Plaza" + "\n Dublin 2";

            //set up the email
            EmailCustomer email = new EmailCustomer();
            email.From = "pembrokechris@gmail.com";                 // company email
            email.To = customerDetail.Email;                        // customer email to send to          
            email.Body = "Dear " + customerDetail.Name + "\n Thank you for bike service/quote request!" + bodyBusinessNameEnding;        
            email.Subject = "Response to Bike repair " + serviceDetail.StatusService + " Request";

            return View("CreateEmail", email);
        }

        //
        // POST: /EmailCustomer/Create
        [HttpPost]
        public ActionResult CreateEmail(EmailCustomer custEmail)
        {
            try
            {
                MailMessage mail = new MailMessage();
                mail.To.Add(custEmail.To);
                mail.From = new MailAddress(custEmail.From);
                mail.Subject = custEmail.Subject;
                string Body = custEmail.Body;
                mail.Body = Body;
                mail.IsBodyHtml = true;
                SmtpClient smtp = new SmtpClient();
                smtp.Host = "smtp.gmail.com";
                smtp.Port = 587;
                smtp.UseDefaultCredentials = false;
                smtp.Credentials = new System.Net.NetworkCredential("pembrokechris@gmail.com", "peMB1234");  // enter senders (own) username and password
                smtp.EnableSsl = true;
                smtp.Send(mail);
            }
            catch (Exception e)   // if un-successful
            {
                ViewBag.emailSendError = "Email not successfully sent!" + e.Message ;
                return View("CreateEmail", custEmail);
            }
            return RedirectToAction("AdminSearches", "BikeService");   // if successful redirect back to searches
        }

        public ActionResult RedirectToAdminSearches()
        {
            return RedirectToAction("AdminSearches", "BikeService");
        }

        //
        // GET: /EmailCustomer/
        public ActionResult Index()
        {
            return View();
        }

        //
        // GET: /EmailCustomer/Details/5
        public ActionResult Details(int id)
        {
            return View();
        }

        //
        // GET: /EmailCustomer/Edit/5
        public ActionResult Edit(int id)
        {
            return View();
        }

        //
        // POST: /EmailCustomer/Edit/5
        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add update logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /EmailCustomer/Delete/5
        public ActionResult Delete(int id)
        {
            return View();
        }

        //
        // POST: /EmailCustomer/Delete/5
        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
